package com.trs.publish.security;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import com.trs.publish.domain.User;
import com.trs.publish.repository.UserRepository;

@Component  
public class CustomUserDetailsService implements UserDetailsService {  

		@Autowired
		private UserRepository userRepository;
		
		//重写loadUserByUsername 方法获得 userdetails 类型用户
	    @Override
	    public UserDetails loadUserByUsername(String username) { 

	        User user = userRepository.findByName(username);	        
	        if(user == null){
	        	throw new UsernameNotFoundException(username+": 用户名不存在");
	        }
	     // SecurityUser实现UserDetails并继承User
	        SecurityUser seu = new SecurityUser(user);  
	        return  seu;
	    }
}